package com.hpin.fansea.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.MyServiceAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.MyServiceList;
import com.hpin.fansea.bean.ServiceInfoVo;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 我的服务
 * Created by Aaron on 15/9/23.
 */
public class MyServiceActivity extends BaseActivity {

    private int currentIndex=0;
    private PullToRefreshListView plv_service;
    private List<ServiceInfoVo> serviceList = new ArrayList<>();
    private MyServiceAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_service);

        initWidget();

        mAdapter = new MyServiceAdapter(this, serviceList);

        plv_service.setAdapter(mAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();

        getMyServiceList();
    }

    private void getMyServiceList() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.SERVICELIST +
                "?token=" + sp.getString(Constant.token, "") + "&pageSize=" + Constant.PAGECOUNT + "&pageIndex=" + (++currentIndex), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                plv_service.onRefreshComplete();

                try {

                    MyServiceList serviceResult = JSONObject.parseObject(str, MyServiceList.class);

                    if (Constant.STATUS_SUCESS.equals(serviceResult.status)) {

                        List<ServiceInfoVo> activityList = serviceResult.result.activityList;

                        if (activityList == null || activityList.size() == 0)
                            return;

                        setDataView(activityList);

                        mAdapter.setServiceList(serviceList);
                        mAdapter.notifyDataSetChanged();

                    } else
                        showToast(serviceResult.message);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, new HttpUtil.FailListener() {
            @Override
            public void setOnFailListener() {

                plv_service.onRefreshComplete();

            }
        });

    }

    protected void setDataView(List<ServiceInfoVo> temp) {

        if (serviceList.size() == 0) {
            serviceList = temp;
            return;
        }

        int nowSize = serviceList.size();
        int newSize = temp.size();
        for (int i = 0; i < newSize; i++) {
            boolean exist = true;
            ServiceInfoVo mcod = temp.get(i);
            for (int j = 0; j < nowSize; j++) {
                ServiceInfoVo nmcod = serviceList.get(j);
                if (mcod.serId.equals(nmcod.serId)) {
                    exist = false;
                    break;
                }
            }
            if (exist)
                serviceList.add(mcod);

        }
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        plv_service = (PullToRefreshListView) findViewById(R.id.plv_service);

        plv_service.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (serviceList==null || serviceList.size()==0)
                    return;

                --position;

                Intent mIntent = new Intent(MyServiceActivity.this, ServiceDetailActivity.class);
                mIntent.putExtra("handleId", serviceList.get(position).id_);
                mIntent.putExtra("handleName", serviceList.get(position).serviceName);
                startActivity(mIntent);

            }
        });

        plv_service.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(MyServiceActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                currentIndex = 0;
                getMyServiceList();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(MyServiceActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                getMyServiceList();

            }
        });

        tv_title.setText(getResources().getString(R.string.my_service));

        iv_left.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

        }

    }
}
